package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.entity.Course;
import com.offcn.entity.CourseDetail;
import com.offcn.service.CourseDetailService;
import com.offcn.service.CourseService;
import com.offcn.service.Impl.CourseDetailServiceImpl;
import com.offcn.service.Impl.CourseServiceImpl;
import com.offcn.utils.PageTool;
import com.offcn.utils.ResultBean;
import com.offcn.utils.UploadUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@WebServlet("/course")
@MultipartConfig
public class CourseServlet extends BaseServlet {
    ResultBean bean = null;

    //    查询所有课程
    protected void findAllCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        String search = request.getParameter("search");
        CourseService service = new CourseServiceImpl();
        int totalCount = service.getTotalCount(search);
        PageTool pageTool = new PageTool(totalCount, currentPage, Integer.parseInt(pageSize));
        List<Course> list = service.findAllCourse(pageTool, search);
        if (list != null) {
            pageTool.setList(list);
            bean = new ResultBean(200, "查询成功", pageTool);
        } else {
            bean = new ResultBean(500, "查询失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    上传文件
    protected void uploadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part part = request.getPart("file");
        String fileName = UploadUtils.uploadFile(part);
        String tail = fileName.substring(fileName.lastIndexOf(".") + 1);
        if ("jpg".equals(tail) || "png".equals(tail)) {
            bean = new ResultBean(1, "图片上传成功", fileName);
        } else if ("mp4".equals(tail)) {
            bean = new ResultBean(2, "视频上传成功", fileName);
        } else {
            bean = new ResultBean(500, "上传失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    删除文件
    protected void delFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getParameter("filename");
        File file = new File("D:/eduSource/" + filename);
        boolean delete = file.delete();
        if (delete) {
            bean = new ResultBean(200, "删除成功", null);
        } else {
            bean = new ResultBean(500, "删除失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    添加课程
    protected void addCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        Course course = new Course();
        BeanUtils.populate(course, map);
        CourseService service = new CourseServiceImpl();
        int i = service.addCourse(course);
        if (i > 0) {
            bean = new ResultBean(200, "添加成功", i);
        } else {
            bean = new ResultBean(500, "添加失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    删除课程
    protected void delAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String[] delarrs = request.getParameterValues("delarr");
        String s = Arrays.toString(delarrs);
        String arr = "(" + s.substring(1, s.length() - 1) + ")";
        CourseService service = new CourseServiceImpl();
        int i = service.dellAll(arr);
        if (i > 0) {
            bean = new ResultBean(200, "删除成功", i);
        } else {
            bean = new ResultBean(500, "删除失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    更新课程
    protected void updateCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        Course course = new Course();
        BeanUtils.populate(course, map);
        CourseService service = new CourseServiceImpl();
        int i = service.updateCourse(course);
        if (i > 0) {
            bean = new ResultBean(200, "添加成功", i);
        } else {
            bean = new ResultBean(500, "添加失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    /***************************************用户页面业务*******************************************************/

//    根据分类查询课程
    protected void findByCourseType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String coursetype = request.getParameter("coursetype");
        int num = Integer.parseInt(request.getParameter("num"));
        System.out.println("接受课程类型为" + coursetype);
        CourseService service = new CourseServiceImpl();
        List<Course> list = service.findByCourseType(coursetype, num);
        if (list.size() > 0 && list != null) {
            System.out.println("查询到结果：" + list.size());
            bean = new ResultBean(200, "查询成功", list);
        } else {
            bean = new ResultBean(500, "查询失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }

    //    根据课程id查询课程
    protected void findCourseByCid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int cid = Integer.parseInt(request.getParameter("cid"));
        CourseService courseService = new CourseServiceImpl();
        Course course = courseService.findByCid(cid);
        CourseDetailService courseDetailService = new CourseDetailServiceImpl();
        List<CourseDetail> list = courseDetailService.findByCid(cid);
        if (null != list && list.size() > 0) {
            course.setList(list);
            bean = new ResultBean(200, "查询成功", course);
        } else {
            bean = new ResultBean(500, "查询失败", null);
        }
        response.getWriter().write(JSON.toJSONString(bean));
    }
}
